package com.seelyn.ddvideo.web;

import com.seelyn.ddvideo.model.Video;
import com.seelyn.ddvideo.model.VideoFrom;
import com.seelyn.ddvideo.model.VideoPlayer;
import com.seelyn.ddvideo.service.SpiderService;
import com.seelyn.ddvideo.service.VideoPrefixService;
import com.seelyn.ddvideo.service.VideoService;
import com.seelyn.ddvideo.spider.FromSiteEnum;
import com.seelyn.ddvideo.spider.MovieSubTypeEnum;
import com.seelyn.ddvideo.spider.MovieTypeEnum;
import com.seelyn.ddvideo.utils.VideoPrefixUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author slacrey
 * @since 2017/6/10
 */
@RestController
@RequestMapping("/api")
public class VideoController {

    @Autowired
    private VideoService videoService;
    @Autowired
    private VideoPrefixService videoPrefixService;
    @Autowired
    private SpiderService spiderService;

    @PostMapping(value = "/video/save")
    public View<String> videoSave(@RequestParam("title") String title, @RequestParam("picUrl") String picUrl,
                                  @RequestParam("fromSite") String fromSite, @RequestParam("score") Float score,
                                  @RequestParam("playUrl") String playUrl) {

        String prefixUrl = VideoPrefixUtils.handlerPrefix(videoPrefixService, fromSite);

        VideoPlayer player = new VideoPlayer();
        player.setName(title);
        player.setOrder(1);
        player.setUrl(prefixUrl + playUrl);

        Video video = videoService.findVideoByTitle(title);
        if (video != null) {
            boolean exist = false;
            for (VideoFrom videoFrom : video.getVideoFroms()) {

                if (fromSite.equals(videoFrom.getFromSite())) {
                    videoFrom.getPlayers().clear();
                    videoFrom.getPlayers().add(player);
                    exist = true;
                }

            }

            if (!exist) {
                VideoFrom videoFrom = new VideoFrom();
                videoFrom.setFromSite(FromSiteEnum.getEnum(fromSite).name());
                videoFrom.setFromSiteName(fromSite);
                videoFrom.getPlayers().add(player);
                video.getVideoFroms().add(videoFrom);
            }

        } else {
            video = new Video();
            video.setTitle(title);
            video.setScore(score);
            video.setFromSite(fromSite);
            video.setOnlineTime(new Date().getTime());
            video.setPicUrl(picUrl);
            video.setSubType(MovieSubTypeEnum.JUQINGPIAN.ordinal());


            VideoFrom videoFrom = new VideoFrom();
            videoFrom.setFromSite(FromSiteEnum.getEnum(fromSite).name());
            videoFrom.setFromSiteName(fromSite);
            videoFrom.getPlayers().add(player);
            video.getVideoFroms().add(videoFrom);

        }


        View<String> view = new View<>();
        view.setCode(View.OK);
        view.setMessage("OK");
        return view;

    }

    @GetMapping(value = "/video/{id}")
    public Video videoDetail(@PathVariable("id") String id) {
        Video video = videoService.findVideoById(id);
        for (VideoFrom videoFrom : video.getVideoFroms()) {
            String prefixUrl = VideoPrefixUtils.handlerPrefix(videoPrefixService, videoFrom.getFromSite());
            for (VideoPlayer player : videoFrom.getPlayers()) {
                player.setUrl(prefixUrl + player.getUrl());
            }
        }
        return video;
    }

    @GetMapping(value = "/video/search")
    public Page<Video> qqVideo(@RequestParam("q") String q, @RequestParam(value = "page", defaultValue = "1") Integer page,
                               @RequestParam(value = "size", defaultValue = "10") Integer size) {

        page = page >= 1 ? (page - 1) : 0;
        Pageable pageable = new PageRequest(page, size, Sort.Direction.DESC, "score");
        return videoService.findVideoByTitleLikeAndMovieTypeLessThanEqual(q, MovieTypeEnum.all.ordinal(), pageable);
    }

    @GetMapping(value = "/video/list/{type}")
    public Page<Video> qqVideo(@PathVariable("type") Integer type,
                               @RequestParam("page") Integer page, @RequestParam("size") Integer size) {

        page = page >= 1 ? (page - 1) : 0;
        type = type >= MovieTypeEnum.av18.ordinal() ? (type - 1) : type;
        Pageable pageable = new PageRequest(page, size, Sort.Direction.DESC, "onlineTime");

        return videoService.findByMovieTypeAndDelFlagFalse(type, pageable);

    }

    @GetMapping(value = "/video/startQQSpider")
    public View<String> startQQSpider(@RequestParam("page") Integer page) {

        spiderService.startQQSpider(page);

        View<String> view = new View<>();
        view.setCode(View.OK);
        view.setMessage("OK");
        return view;
    }

    @GetMapping(value = "/video/startYouKuSpider")
    public View<String> startYouKuSpider(@RequestParam("page") Integer page) {

        spiderService.startYouKuSpider(page);

        View<String> view = new View<>();
        view.setCode(View.OK);
        view.setMessage("OK");
        return view;
    }

    @GetMapping(value = "/video/startKuyunSpider")
    public View<String> startKuyunSpider(@RequestParam("page") Integer page) {

        spiderService.startKuyunSpider(page);

        View<String> view = new View<>();
        view.setCode(View.OK);
        view.setMessage("OK");
        return view;
    }

    @GetMapping(value = "/video/startM3u8Spider")
    public View<String> startM3u8Spider(@RequestParam("page") Integer page) {

        spiderService.startM3u8Spider(page);

        View<String> view = new View<>();
        view.setCode(View.OK);
        view.setMessage("OK");
        return view;
    }

}
